package com.guli.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.eduservice.bean.EduCourse;
import com.guli.eduservice.bean.vo.CourseInfoVo;
import com.guli.eduservice.bean.vo.CoursePublishVo;
import com.guli.eduservice.bean.vo.CourseQuery;
import com.guli.eduservice.client.UcentClient;
import com.guli.eduservice.service.EduChapterService;
import com.guli.eduservice.service.EduCourseDescriptionService;
import com.guli.eduservice.service.EduCourseService;
import com.guli.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/eduservice/educourse")
@CrossOrigin
public class EduCourseController {
    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Resource
    private EduVideoService eduVideoService;

    @Resource
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据条件进行分页查询课程的方法")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageListCourseCondition(@PathVariable(value = "current") Integer current,
                                     @PathVariable(value = "limit") Integer limit,
                                     @RequestBody(required = false) CourseQuery CourseQuery) {
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current, limit);

        //wrapper添加条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询（mybatis 动态sql 判断是否为空来进行拼接）
        if(CourseQuery != null) {
            String title = CourseQuery.getTitle();
            String status = CourseQuery.getStatus();
            String begin = CourseQuery.getBegin();
            String end = CourseQuery.getEnd();
            String teacherId = CourseQuery.getTeacherId();
            String subjectParentId = CourseQuery.getSubjectParentId();
            String subjectId = CourseQuery.getSubjectId();


            if (!StringUtils.isEmpty(title)) {
                wrapper.like("title", title);
            }
            if (!StringUtils.isEmpty(status)) {
                wrapper.eq("status", status);
            }
            if (!StringUtils.isEmpty(teacherId)) {
                wrapper.like("teacher_id", teacherId);
            }
            if (!StringUtils.isEmpty(subjectParentId)) {
                wrapper.eq("subject_parent_id", subjectParentId);
            }
            if (!StringUtils.isEmpty(subjectId)) {
                wrapper.eq("subject_id", subjectId);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                wrapper.le("gmt_create", end);
            }
        }
        wrapper.orderByDesc("gmt_create");
        //调用page方法来获取分页信息
        eduCourseService.page(pageCourse, wrapper);
        long total = pageCourse.getTotal(); //总记录数
        List<EduCourse> records = pageCourse.getRecords(); // 数据list集合
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);

        return R.ok().data(map);
    }

    @ApiOperation("新增课程基本信息")
    @PostMapping("addEduCourse")
    public R addEduCourse(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = eduCourseService.saveEduCourse(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    @ApiOperation("根据课程id查询基本信息")
    @GetMapping("getEduCourse/{courseId}")
    public R getEduCourse(@PathVariable("courseId")String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getEduCourse(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation("修改课程基本信息")
    @PostMapping("updateEduCourse")
    public R updateEduCourse(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateEduCourse(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getCoursePublishVo/{id}") public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo courseInfoForm = eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);

    }

    @ApiOperation("修改课程发布状态（已发布）Normal")
    @PostMapping("updateStatusById/{id}")
    public R updateStatusById(@PathVariable("id") String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean update = eduCourseService.updateById(eduCourse);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("deleteCourseById/{courseId}")
    public R deleteCourseById(@PathVariable("courseId") String courseId){
        //1.删除小节
        boolean b = eduVideoService.deletVideoByCourseId(courseId);
        //2.删除章节
        boolean b1 = eduChapterService.deleteChapterByCourseId(courseId);
        //3.删除课程描述简介
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程
        boolean b2 = eduCourseService.removeById(courseId);
        if(b2){
            return R.ok();
        }else{
            return R.error();
        }

    }

}

